package com.dd.ms.consumer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.ms.provider.api.DataObjectRpcApi;
import com.dd.ms.provider.pojo.DataObjectBO;
import com.dd.ms.common.response.ApiResponse;
import com.dd.ms.common.pojo.annotation.Attention;
import com.dd.ms.consumer.service.internal.ConsumerServiceImpl;
import com.dd.ms.provider.pojo.req.DataObjectPageQueryReq;
import com.dd.ms.provider.pojo.view.DataObjectVO;
import com.dd.ms.storage.api.StorageRpcApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/3/12 19:18
 * @Description ConsumerController
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/consumer")
@Api(tags = "消费者实践接口列表", description = "接口内部包含对本地、Feign、Dubbo等数据源接口的调用")
public class ConsumerController {

    @DubboReference(interfaceClass = DataObjectRpcApi.class, group = "DUBBO_DD_MS_PROVIDER", version = "1.0")
    private DataObjectRpcApi dataObjectRpcApi;

    @DubboReference(interfaceClass = StorageRpcApi.class, group = "DUBBO_DD_MS_STORAGE", version = "1.0")
    private StorageRpcApi storageRpcApi;

    @Attention("Feign调用")
    private final ConsumerServiceImpl consumerServiceImpl;

    @Attention("Dubbo调用")
    @GetMapping("/data-object/{id}")
    @ApiOperation("调用Dubbo接口·通过id获取DataObjectDO对象")
    public ApiResponse<DataObjectBO> get(@ApiParam("DataObjectDO数据库自增id") @PathVariable Long id){
        log.info("C DubboConsumerController M get() id = {}", id);
        DataObjectBO result = dataObjectRpcApi.getOneById(id);
        log.info("C DubboConsumerController M get() result = {}", result);
        return ApiResponse.success(result);
    }

    @Attention("Feign调用")
    @PostMapping("/data-object/page")
    @ApiOperation("调用Feign接口·获取DataObject分页信息")
    public ApiResponse<Page<DataObjectVO>> page(@RequestBody DataObjectPageQueryReq req) {
        return ApiResponse.success(consumerServiceImpl.pageVO(req));
    }
}
